package org.example.hadoop3;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;

/**
 * 开发步骤：
 * 获取客户端对象
 * 调用相关方法实现功能
 * 关闭
 */
public class TestHDFS {

    private URI uri;
    private Configuration conf;
    private String user;
    private FileSystem fs;

    @Before
    public void init() throws IOException, InterruptedException {
        uri = URI.create("hdfs://192.168.110.97:9000");
        conf = new Configuration();
        user = "root";
        fs = FileSystem.get(uri, conf, user);
    }

    @After
    public void uninit() throws IOException {
        fs.close();
    }

    //@Test
    public void testIO() throws IOException {
        String srcFile = "/Users/gaoqiang/dev/xsync.sh";
        String destFile = "/testhdfs/xsync2.sh";
        FileInputStream fis = new FileInputStream(new File(srcFile));
        FSDataOutputStream fos = fs.create(new Path(destFile));

        // fs.open(new Path(srcFile))

        // copy
//        int i;
//        byte [] buffer = new byte[1024];
//        while ((i = fis.read(buffer)) != -1) {
//            fos.write(buffer, 0, i);
//        }
        // hadoop copy
        IOUtils.copyBytes(fis, fos, conf);

        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }

    @Test
    public void testUpload() throws IOException {
        fs.copyFromLocalFile(false, true,
                new Path("/Users/gaoqiang/dev/xsync.sh"),
                new Path("/test/"));
    }

    @Test
    public void testDownload() throws IOException {
        fs.copyToLocalFile(false,
                new Path("/test/xsync.sh"),
                new Path("/Users/gaoqiang/dev/xsync1.sh"),
                false);
    }

    @Test
    public void testRename() throws IOException {
        fs.rename(new Path("/testhdfs/xsync.sh"),
                new Path("/testhdfs/xsyncnew.sh")); // 移动/xsync1.sh
    }

    @Test
    public void testListFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> listFiles =
                fs.listFiles(new Path("/testhdfs"), false);
        while (listFiles.hasNext()) {
            LocatedFileStatus fileStatus = listFiles.next();
            System.out.println("path: " + fileStatus.getPath());
            System.out.println("auth: " + fileStatus.getPermission());
            System.out.println("owner: " + fileStatus.getOwner());
            System.out.println("group: " + fileStatus.getGroup());
            System.out.println("size: " + fileStatus.getLen());
            System.out.println("rep: " + fileStatus.getReplication());
            System.out.println("block size: " + fileStatus.getBlockSize());
            System.out.println("pos: " + Arrays.toString(fileStatus.getBlockLocations()));
        }
    }

    @Test
    public void testDelete() throws IOException {
        fs.delete(new Path("/testhdfs"), false);
    }

    @Test
    public void testListStatus() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/testhdfs"));
        for (FileStatus fileStatus : fileStatuses) {
            if (fileStatus.isFile()) {
                System.out.println("File: " + fileStatus.getPath());
            } else {
                System.out.println("Dir: " + fileStatus.getPath());
            }
        }
    }

    @Test
    public void testHDFS() {
        try {
            FileSystem fs = FileSystem.get(uri, conf, user);
            System.out.println("fs=" + fs);
            boolean isSuccess = fs.mkdirs(new Path("/testhdfs"));
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
